package ipebg.mzk.common.util;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class NetUtils {
	//-----------------------获取服务器端数据 String格式--------------------------
	public static String returnData(String input, String myurl) {
		try {
			URL url = new URL(myurl);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod("POST");// 提交模式
	        conn.setConnectTimeout(10000);//连接超时 单位毫秒
	        // conn.setReadTimeout(2000);//读取超时 单位毫秒
	        conn.setDoOutput(true);// 是否输入参数
			
	        OutputStream outputStream=conn.getOutputStream();
	        outputStream.write(input.getBytes("utf-8"));
	        outputStream.flush();//写完后在传送，防止某些服务器上数据写不完整就跨域发送

			System.out.println("Server ResponseCode------"+conn.getResponseCode());
			if (conn.getResponseCode() == 200) {
				BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8"));
				String line;
				StringBuilder builder=new StringBuilder();
				while ((line = br.readLine()) != null) {
					builder.append(line);
				}
				br.close();
				return builder.toString();
			}
			conn.disconnect();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	//-----------------------获取服务器端数据 byte流格式--------------------------
	public static byte[] getRemoteData(String input,String myurl) {
		try {
			URL url = new URL(myurl);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod("POST");// 提交模式
	        conn.setConnectTimeout(10000);//连接超时 单位毫秒
	        // conn.setReadTimeout(2000);//读取超时 单位毫秒
	        conn.setDoOutput(true);// 是否输入参数
			
	        OutputStream outputStream=conn.getOutputStream();
	        outputStream.write(input.getBytes("utf-8"));
	        outputStream.flush();//写完后在传送，防止某些服务器上数据写不完整就跨域发送

	        System.out.println("Server ResponseCode------"+conn.getResponseCode());
			if (conn.getResponseCode() == 200) {
				return changeInputStream(conn.getInputStream());
			}
			conn.disconnect();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	private static byte[] changeInputStream(InputStream inputStream) {
		// TODO Auto-generated method stub
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		int len = 0;
		byte[] data = new byte[1024];
		try {
			while ((len = inputStream.read(data)) != -1) {
				outputStream.write(data, 0, len);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		//String returnStr=new String(outputStream.toByteArray(),"utf-8");
		return outputStream.toByteArray();
	}
	
	//--------------------主函数    测试相关方法------------------------------
	public static void main(String[] args) {
		String params="to=yao.chang@mail.foxconn.com&subject=测试邮件&content=测试邮件测试邮件";
		String result=NetUtils.returnData(params, "http://10.207.248.102/emm/MailUtil");
		System.out.println(result);
	}
}
